package com.waimai.mobile.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.Food;
import com.waimai.mobile.api.entity.OrderFood;
import com.waimai.mobile.api.entity.Restaurant;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.fragment.RestaurantCommentFragment;
import com.waimai.mobile.ui.fragment.RestaurantCommentFragment_;
import com.waimai.mobile.ui.fragment.RestaurantDetailFragment;
import com.waimai.mobile.ui.fragment.RestaurantDetailFragment_;
import com.waimai.mobile.ui.fragment.RestaurantOrderFragment;
import com.waimai.mobile.ui.fragment.RestaurantOrderFragment_;
import com.waimai.mobile.ui.fragment.RestaurantTabFragment;

import org.androidannotations.annotations.AfterExtras;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lin on 2015/6/13.
 */

@EActivity(R.layout.activity_restaurant)
public class RestaurantActivity extends BaseActivity02{

    @Extra("restaurantId")
    public Integer restaurantId;

    public Restaurant restaurant;

    @RestService
    public WaiMaiServer server;

    @ViewById(R.id.restaurantViewPager)
    public ViewPager restaurantViewPager;

    @FragmentById(R.id.tabRestaurantFragment)
    public RestaurantTabFragment tabRestaurantFragment;

    @AfterViews
    public void afterViews(){
        loading();
        loadRestaurant(restaurantId);
    }

    @Override
    public void onOptionMenuCreated() {
        super.onOptionMenuCreated();
        setActionRightVisible(true);
        setActionRightIcon(R.mipmap.ic_star);
        setActionBarRightClickListener(new ActionBarRightClickListener() {
            @Override
            public void onClick() {
                Toast.makeText(getApplicationContext(), "暂未开放该功能", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void initViewPager(){
        restaurantViewPager.setVisibility(View.VISIBLE);

        final List<Fragment> fragments = new ArrayList<Fragment>();
        RestaurantOrderFragment restaurantOrderFragment = new RestaurantOrderFragment_();
        RestaurantCommentFragment restaurantCommentFragment = new RestaurantCommentFragment_();
        RestaurantDetailFragment restaurantDetailFragment = new RestaurantDetailFragment_();

        fragments.add(restaurantOrderFragment);
        fragments.add(restaurantCommentFragment);
        fragments.add(restaurantDetailFragment);

        restaurantViewPager.setOffscreenPageLimit(3);
        restaurantViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        tabRestaurantFragment.setOnTabSelectedListtener(new RestaurantTabFragment.OnTablSelectedListener() {
            @Override
            public void onTablSelected(int position, ViewGroup view) {
                switch (position) {
                    case 0:
                        restaurantViewPager.setCurrentItem(position, true);
                        break;
                    case 1:
                        restaurantViewPager.setCurrentItem(position, true);
                        break;
                    case 2:
                        restaurantViewPager.setCurrentItem(position, true);
                        break;
                }
            }
        });

        restaurantViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabRestaurantFragment.selectItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @UiThread
    public void loading(){
        setActionBarTitle("正在加载餐厅...");
        restaurantViewPager.setVisibility(View.GONE);
    }

    @Background
    public void loadRestaurant(Integer restaurantId){
        ResponseModel<Restaurant> model = null;
        try{
            model = server.resturant(restaurantId);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_GET_DATA_SUCCESS:
                        loadedSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_GET_DATA_NONE:
                        loadNoneData();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void loadedSuccess(Restaurant restaurant){
        this.restaurant = restaurant;
        setActionBarTitle(this.restaurant.getRestaurantName());
        initViewPager();
    }

    @UiThread
    public void loadNoneData() {
        setActionBarTitle("餐厅不存在,请返回重试");
    }

    @UiThread
    public void serverError(){
        setActionBarTitle("服务器异常,请返回重试");
    }

    @UiThread
    public void netError(){
        setActionBarTitle("网络错误,请返回重试");
    }

    public void refreshData(){
        loading();
        loadRestaurant(restaurantId);
    }
}
